package com.github.cyf.async.config;

import com.github.cyf.async.context.OauthContext;
import com.github.cyf.async.entity.LoginVal;
import org.springframework.core.task.TaskDecorator;

/**
 * @author chenyifan
 * @create 2025-05-08 13:27
 */
public class ContextTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        LoginVal loginVal = OauthContext.get();
        return () -> {
            try {
                // 设置主线程的上下文信息到子线程中
                OauthContext.set(loginVal);
                runnable.run();
            } finally {
                OauthContext.clear();
            }
        };
    }
}
